const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const extractCss = new ExtractTextPlugin('css/[name]-one.css');
const extractScss = new ExtractTextPlugin('css/[name]-two.css');

module.exports = {
    entry: './src/app.jsx',
    output: {
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/dist/',
        filename: 'js/app.js'
    },
    module: {
        rules: [
            // react语法处理
            {
                test: /\.jsx$/,
                exclude: /(node_modules)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['env', 'react']
                    }
                }
            },
            // css文件处理
            {
                test: /\.css$/,
                use: extractCss.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            // sass文件处理
            {
                test: /\.scss$/,
                use: extractScss.extract({
                    fallback: 'style-loader',

                    use: ['css-loader', 'sass-loader']
                })
            },
            // 图片字体处理
            {
                test: /\.(png|jpg|gif)$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        // 文件大于8k才转为文件
                        limit: 8192,
                        name: 'resource/[name].[ext]'
                    }
                }]
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf|svg)$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        // 文件大于8k才转为文件
                        limit: 8192,
                        name: 'resource/[name].[ext]'
                    }
                }]
            }

        ],
    },
    resolve:{
        alias:{
            page:path.resolve(__dirname, 'src/page'),
            component:path.resolve(__dirname, 'src/component'),
            util:path.resolve(__dirname, 'src/util'),
            service:path.resolve(__dirname, 'src/service'),
        }
        },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html',
            favicon:'./favicon.ico'
        }),
        extractCss,
        extractScss,
        // 提出公共模块
        new webpack.optimize.CommonsChunkPlugin({
            name: 'common',
            filename: 'js/base.js'
        })
    ],

    devServer: {
        port: 8086,
        // 通过historyApiFallback判断若为404则跳转到指定页面
        historyApiFallback:{
            index:'/dist/index.html'
        },
        proxy:{
            // 通过webpack自带devserver配置代理 将/manage的都指定到http://admintest.happymmall.com
            '/manage':{
                target:'http://admintest.happymmall.com',
                // 通过changeOrigin：true实现伪装请求是从http://admintest.happymmall.com发出而不是localhost
                changeOrigin:true
            },
             // 通过webpack自带devserver配置代理 将/manage的都指定到http://admintest.happymmall.com
             '/user/logout.do':{
                target:'http://admintest.happymmall.com',
                // 通过changeOrigin：true实现伪装请求是从http://admintest.happymmall.com发出而不是localhost
                changeOrigin:true
            }
        }
    },
};